<%@page
	import="com.bkav.portal.portlet.permission.productCategoryPermission"%>
<%@page import="com.liferay.portal.kernel.util.HtmlUtil"%>
<%@page import="com.bkav.portal.portlet.product.model.Product"%>
<%@page import="com.bkav.portal.portlet.permission.productPermission"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page
	import="com.bkav.portal.portlet.product.service.ProductCategoryLocalServiceUtil"%>
<%@page
	import="com.bkav.portal.portlet.product.service.ProductCategoryLocalService"%>
<%@page import="com.bkav.portal.portlet.product.model.ProductCategory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/html/product_admin/init.jsp"%>
<%
	String categoryCommand = ParamUtil.getString(request,
	"categoryCommand");
	int productCategoryIndex = 0;
	long categoryId = ParamUtil.getLong(request, "categoryId", 0L);
	String productCategorySearchKeyword = ParamUtil.getString(request, "productCategorySearchKeyword");
	ProductCategory category = null;
	if (categoryId > 0L) {
		category = ProductCategoryLocalServiceUtil.getProductCategory(categoryId);
	}
	List<ProductCategory> productCategoryList = ProductCategoryLocalServiceUtil.findByKeyword(groupId, productCategorySearchKeyword, 0, 10);
%>
<!-- category -->
<div class="category">

	<liferay-portlet:renderURL var="productCategoryAdminHomePageURL">
		<liferay-portlet:param name="jspPage"
			value="/html/product_admin/view.jsp" />
		<liferay-portlet:param name="tabs1" value="category" />
	</liferay-portlet:renderURL>

	<liferay-portlet:renderURL var="productCategoryInsertFormURL">
		<liferay-portlet:param name="jspPage"
			value="/html/product_admin/view.jsp" />
		<liferay-portlet:param name="tabs1" value="category" />
		<liferay-portlet:param name="categoryCommand" value="updateCategory" />
	</liferay-portlet:renderURL>

	<!-- lfr-header-row-content -->
	<div class="lfr-header-row">
		<div class="lfr-header-row-content">
			<!-- add_new_category_button -->
			<div class="add_new_category_button" align="left">
				<c:if
					test="<%=productCategoryPermission.contains(permissionChecker, groupId, productCategoryPermission.ADD)%>">
					<aui:button type="button" value="Thêm chuyên mục"
						href="<%=productCategoryInsertFormURL%>" name="categoryButton" />
				</c:if>
				<c:if
					test="<%=productCategoryPermission.contains(permissionChecker, groupId, productCategoryPermission.PERMISSIONS)%>">
					<liferay-security:permissionsURL
						modelResource="<%=ProductCategory.class.getName()%>"
						modelResourceDescription="<%=HtmlUtil.escape(themeDisplay.getScopeGroupName())%>"
						resourcePrimKey="<%=String.valueOf(groupId)%>"
						var="permissionsURL" />
					<aui:button name="productPermissionsBtn" href="<%=permissionsURL%>" value="permissions" />
				</c:if>
			</div>

			<!-- .product_category_search_form -->
			<div class="product_category_search_form" align="right">
				<aui:form name="product_category_search_form" method="post"
					action="<%=productCategoryAdminHomePageURL%>">
					<input type="text" id="<portlet:namespace/>categorySearchInput"
						name="productCategorySearchKeyword"
						value='<%=productCategorySearchKeyword%>' />
					<aui:button type="submit" value="Tìm kiếm" />
					<aui:button type="button" value="Hiển thị toàn bộ"
						href="<%=productCategoryAdminHomePageURL%>" />
				</aui:form>
			</div>
		</div>
	</div>

	<c:if test='<%=Validator.equals(categoryCommand, "updateCategory")%>'>
		<!-- productCategory-update-form -->
		<div class="productCategory-update-form" align="center">
			<liferay-portlet:actionURL name="updateProductCategory" var="updateProductCategoryActionURL">
				<liferay-portlet:param name="categoryId" value="<%=String.valueOf(categoryId)%>" />
				<liferay-portlet:param name="redirectURL" value="<%=productCategoryAdminHomePageURL%>" />
			</liferay-portlet:actionURL>
			<aui:form name="category_update_form" method="post"
				action="<%=updateProductCategoryActionURL%>">
				<table>
					<tr>
						<td>Tên chuyên mục</td>
						<td><aui:input type="text" label="" name="categoryName"
								value='<%=(category != null) ? category
								.getCategoryName() : ""%>'></aui:input>
						</td>
					</tr>
					<tr>
						<td>Thông tin chuyên mục</td>
						<td><aui:input type="textarea" label="" name="categoryInfo"
								value='<%=(category != null) ? category
								.getCategoryInfo() : ""%>'></aui:input>
						</td>
					</tr>
					<tr>
						<td></td>
						<c:if test="<%=productCategoryPermission.contains(permissionChecker, groupId, productCategoryPermission.UPDATE) %>">
						    <td><aui:button name="save" value="Save" type="submit"></aui:button>
							   <aui:button name="cancle" value="Cancle" type="button"></aui:button>
						    </td>
					    </c:if>
					</tr>
				</table>
			</aui:form>
		</div>
	</c:if>
    <div class="product_categories">
		
		<liferay-ui:search-container>
			
			<liferay-ui:search-container-results
			    results="<%=ListUtil.subList(productCategoryList, searchContainer.getStart(), searchContainer.getEnd()) %>"
			    total="<%=productCategoryList.size() %>"
			/>
				
			<liferay-ui:search-container-row className="ProductCategory" keyProperty="categoryId" modelVar="Category" >
				<%
					productCategoryIndex++;
					
					int productCategoryCounter = ProductCategoryLocalServiceUtil.countAll();
				%>
				<liferay-portlet:renderURL var="updateCategoryURL" >				
					<liferay-portlet:param name="jspPage" value="/html/product_admin/view.jsp"/>
					<liferay-portlet:param name="tabs1" value="category"/>
					<liferay-portlet:param name="categoryCommand" value="updateCategory"/>
					<liferay-portlet:param name="categoryId" value="<%=String.valueOf(Category.getCategoryId())%>"/>
				</liferay-portlet:renderURL>
								
				<liferay-ui:search-container-column-text name="#" value="<%=String.valueOf(productCategoryIndex) %>"/>
					
				<liferay-ui:search-container-column-text name="Tên chuyên mục" value="<%=Category.getCategoryName()%>" href="<%=updateCategoryURL %>"/>
				
				<liferay-ui:search-container-column-text name="Thông tin chuyên mục" value="<%=Category.getCategoryInfo()%>" href="<%=updateCategoryURL %>"/>
				
				<liferay-ui:search-container-column-text name="Ngày tạo" value="<%=dateFormat.format(Category.getCreateDate())%>"/>
				
				<liferay-ui:search-container-column-text name="Tài khoản tạo" value="<%=Category.getUserName()%>"/>
				
				<liferay-ui:search-container-column-text name="edit">
					<c:if test="<%=productCategoryPermission.contains(permissionChecker, groupId, productCategoryPermission.UPDATE)%>">														
						<liferay-ui:icon image="edit" message="edit" url="<%= updateCategoryURL %>" />
					</c:if>
				</liferay-ui:search-container-column-text>
								
				<liferay-ui:search-container-column-text name="delete">
					<c:if test="<%=productCategoryPermission.contains(permissionChecker, groupId, productCategoryPermission.DELETE)%>">															
						<liferay-portlet:actionURL  name="deleteProductCategory" var="deleteCategoryURL">
							<liferay-portlet:param name="categoryId" value="<%=String.valueOf(Category.getCategoryId())%>"/>
							<liferay-portlet:param name="redirectURL" value="<%=productCategoryAdminHomePageURL%>"/>
						</liferay-portlet:actionURL>
						
						<liferay-ui:icon-delete url="<%= deleteCategoryURL %>" />
					</c:if>
				</liferay-ui:search-container-column-text>
			</liferay-ui:search-container-row>
			
			<liferay-ui:search-iterator paginate="<%=false %>"/>
		</liferay-ui:search-container>
	</div>
	<!-- product_categories -->
	
</div>



<script type="text/javascript">
	AUI()
			.ready(
					'aui-base,aui-form-validator,aui-overlay-context-panel',
					function(A) {

						var categorySearchInput = A
								.one('#<portlet:namespace/>categorySearchInput');

						if (categorySearchInput) {
							categorySearchInput.focus();
						}

						if (<%=Validator.equals(categoryCommand, "updateCategory")%>) {

							var categoryNameInput = A
									.one('#<portlet:namespace />categoryName');

							if (categoryNameInput) {
								categoryNameInput.focus();
							}

							window.validator = new A.FormValidator(
									{

										boundingBox : document.<portlet:namespace />category_update_form,

										rules : {
											<portlet:namespace />categoryName : {
												required : true,
												rangeLength : [ '3', '300' ]
											},
											<portlet:namespace />categoryInfo : {
												required : false,
												rangeLength : [ '0', '450' ]
											}
										}
									});
						}
					});
</script>


